package com.ruoyi.manage.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.manage.domain.WorkChange;
import com.ruoyi.manage.mapper.NursSchedulMapper;
import com.ruoyi.manage.domain.NursSchedul;
import com.ruoyi.manage.service.INursSchedulService;

/**
 * 护士排班Service业务层处理
 * 
 * @author liuyangyang
 * @date 2025-06-10
 */
@Service
public class NursSchedulServiceImpl implements INursSchedulService 
{
    @Autowired
    private NursSchedulMapper nursSchedulMapper;

    /**
     * 查询护士排班
     * 
     * @param nursSchedulId 护士排班主键
     * @return 护士排班
     */
    @Override
    public NursSchedul selectNursSchedulByNursSchedulId(Long nursSchedulId)
    {
        return nursSchedulMapper.selectNursSchedulByNursSchedulId(nursSchedulId);
    }

    /**
     * 查询护士排班列表
     * 
     * @param nursSchedul 护士排班
     * @return 护士排班
     */
    @Override
    public List<NursSchedul> selectNursSchedulList(NursSchedul nursSchedul)
    {
        return nursSchedulMapper.selectNursSchedulList(nursSchedul);
    }

    /**
     * 新增护士排班
     * 
     * @param nursScheduls 护士排班
     * @return 结果
     */
    @Transactional
    @Override
    public void insertNursSchedul(List<NursSchedul> nursScheduls)
    {
        nursScheduls.forEach(nursSchedul -> {

            if (nursSchedul.getNursSchedulId() != null) {
                updateNursSchedul(nursSchedul);
            }else{
                nursSchedul.setCreateTime(DateUtils.getNowDate());
                nursSchedulMapper.insertNursSchedul(nursSchedul);
                insertWorkChange(nursSchedul);
            }

        });


    }

    /**
     * 修改护士排班
     * 
     * @param nursSchedul 护士排班
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNursSchedul(NursSchedul nursSchedul)
    {

        nursSchedul.setUpdateTime(DateUtils.getNowDate());
        nursSchedulMapper.deleteWorkChangeByNursSchedulId(nursSchedul.getNursSchedulId());
        insertWorkChange(nursSchedul);
        return nursSchedulMapper.updateNursSchedul(nursSchedul);
    }

    /**
     * 批量删除护士排班
     * 
     * @param nursSchedulIds 需要删除的护士排班主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursSchedulByNursSchedulIds(Long[] nursSchedulIds)
    {
        nursSchedulMapper.deleteWorkChangeByNursSchedulIds(nursSchedulIds);
        return nursSchedulMapper.deleteNursSchedulByNursSchedulIds(nursSchedulIds);
    }

    /**
     * 删除护士排班信息
     * 
     * @param nursSchedulId 护士排班主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursSchedulByNursSchedulId(Long nursSchedulId)
    {
        nursSchedulMapper.deleteWorkChangeByNursSchedulId(nursSchedulId);
        return nursSchedulMapper.deleteNursSchedulByNursSchedulId(nursSchedulId);
    }

    /**
     * 新增交接班信息
     * 
     * @param nursSchedul 护士排班对象
     */
    public void insertWorkChange(NursSchedul nursSchedul)
    {
        List<WorkChange> workChangeList = nursSchedul.getWorkChangeList();
        Long nursSchedulId = nursSchedul.getNursSchedulId();
        if (StringUtils.isNotNull(workChangeList))
        {
            List<WorkChange> list = new ArrayList<WorkChange>();
            for (WorkChange workChange : workChangeList)
            {
                workChange.setNursSchedulId(nursSchedulId);

                if(workChange.getUserId()!=null){
                    list.add(workChange);
                }

            }
            if (list.size() > 0)
            {
                nursSchedulMapper.batchWorkChange(list);
            }
        }
    }
}
